{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 9.Palindrome Number 回文数\n",
    "\n",
    "### 难度：Easy\n",
    "\n",
    "## 刷题内容\n",
    "\n",
    "> 原题链接\n",
    "\n",
    " - 中文：https://leetcode-cn.com/problems/palindrome-number/description\n",
    " - 英文：https://leetcode.com/problems/palindrome-number\n",
    " \n",
    "> 内容描述\n",
    "\n",
    "```\n",
    "判断一个整数是否是回文数。回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。\n",
    "\n",
    "示例 1:\n",
    "\n",
    "输入: 121\n",
    "输出: true\n",
    "\n",
    "示例 2:\n",
    "\n",
    "输入: -121\n",
    "输出: false\n",
    "解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。\n",
    "\n",
    "示例 3:\n",
    "\n",
    "输入: 10\n",
    "输出: false\n",
    "解释: 从右向左读, 为 01 。因此它不是一个回文数。\n",
    "\n",
    "进阶:\n",
    "\n",
    "你能不将整数转为字符串来解决这个问题吗？\n",
    "```\n",
    "\n",
    "## 解题方案\n",
    "\n",
    "> 思路 1\n",
    "\n",
    " - 首先负数肯定不是回文数\n",
    " - 通过字符串进行反转，再转化为数字，对比数字是否相等就行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "    def isPalindrome(self, x):\n",
    "        \"\"\"\n",
    "        :type x: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        # 如果是负数，肯定不是回文数，排除掉\n",
    "        if x < 0:\n",
    "            return False\n",
    "        # 我们将 数字转为 str ，然后反转，再转化为 int，比较，相等的话，就是回文数，不相等的话，就不是回文数\n",
    "        elif x != int(str(x)[::-1]):\n",
    "            return False\n",
    "        else:\n",
    "            return True\n",
    "        \n",
    "s = Solution()\n",
    "x = 2345\n",
    "y = 121\n",
    "print(s.isPalindrome(x))\n",
    "print(s.isPalindrome(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 思路 2\n",
    "\n",
    " - 负数一定不是回文数\n",
    " - 如果一个数字是正数，并且能被我 0 整除那它肯定也不是 palidrome\n",
    " - 这样我们就降低了复杂度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "    def isPalindrome(self, x):\n",
    "        \"\"\"\n",
    "        :type x: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        # 处理特殊情况：\n",
    "        # 1、负数一定不是回文数\n",
    "        # 2、如果数字的最后一位是 0， 为了让数字是回文数字，则第一位数字也应该是 0，只有 0 满足这种情况\n",
    "        if x < 0 or (x % 10 == 0 and x is not 0):\n",
    "            return False\n",
    "        revertNumber = 0\n",
    "        while x > revertNumber:\n",
    "            revertNumber = revertNumber * 10 + x % 10\n",
    "            x /= 10\n",
    "        # 当数字长度为奇数时，我们可以通过 revertedNumber/10 去除处于中位的数字。\n",
    "        # 例如，当输入为 12321 时，在 while 循环的末尾我们可以得到 x = 12，revertedNumber = 123，\n",
    "        # 由于处于中位的数字不影响回文（它总是与自己相等），所以我们可以简单地将其去除。\n",
    "        return x == revertNumber or x == revertNumber/10\n",
    "\n",
    "s = Solution()\n",
    "x = 2345\n",
    "y = 121\n",
    "print(s.isPalindrome(x))\n",
    "print(s.isPalindrome(y))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
